<?php
include('../include/config.php');
	$err = "";
	if(@$_POST['addStatie']){
			if( (@$_POST['cod_statie']) && (@$_POST['lat']) && (@$_POST['tipsenzor']) && (@$_POST['lat']) && (@$_POST['lng']) && (@$_POST['o_r'])){
			
			if(@$_POST['adaugaTara']){
				$cod_tara = $_POST['adaugaTara'];
				$cod_tara = strtoupper($cod_tara);
				$resvf = mysql_query("select * from tari where cod_tara = '$cod_tara'");
				if(mysql_num_rows($resvf) == 0){
					mysql_query("insert into tari (id_tara,cod_tara) values (null,'$cod_tara') ");
					$id_tara = mysql_insert_id();
					$adaugaTara = $_POST['adaugaTara'];
					$cod_statie = $_POST['cod_statie'];
					$lat = $_POST['lat'];
					$lng = $_POST['lng'];
					$o_r = $_POST['o_r'];
					$tipsenzor = $_POST['tipsenzor'];
					mysql_query("insert into statii (id_statie,id_tara,cod_statie,lat,lng,o_r) values (null,'$id_tara','$cod_statie','$lat','$lng','$o_r')");
					$id_statie = mysql_insert_id();

					foreach($tipsenzor as $val){
						for($i=0;$i<=2;$i++){
							switch($i){
								case "0":
									$cod_senzor = $val . "E";
									mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
								break;
								case "1":
									$cod_senzor = $val . "N";
									mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
								break;
								case "2":
									$cod_senzor = $val . "Z";
									mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
								break;
							}
						}
					}
				}else{
					$err = '<div class="alert alert-danger">Acest cod <b>('.$cod_tara.')</b> de tara exista deja.</div>';
				}
				
				
			}else{
				$id_tara = $_POST['id_tara'];
				$adaugaTara = $_POST['adaugaTara'];
				$cod_statie = $_POST['cod_statie'];
				$lat = $_POST['lat'];
				$lng = $_POST['lng'];
				$o_r = $_POST['o_r'];
				$tipsenzor = $_POST['tipsenzor'];
				mysql_query("insert into statii (id_statie,id_tara,cod_statie,lat,lng,o_r) values (null,'$id_tara','$cod_statie','$lat','$lng','$o_r')");
				$id_statie = mysql_insert_id();

				foreach($tipsenzor as $val){
					for($i=0;$i<=2;$i++){
						switch($i){
							case "0":
								$cod_senzor = $val . "E";
								mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
							break;
							case "1":
								$cod_senzor = $val . "N";
								mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
							break;
							case "2":
								$cod_senzor = $val . "Z";
								mysql_query("insert into senzori (id_senzor,id_statie,cod_senzor) values (null,'$id_statie','$cod_senzor')");
							break;
						}
					}
				}
			}
			
		}else{
			$err = '<div class="alert alert-danger">Va rugam completati toate campurile!</div>';
		}

	}

?>
<!DOCTYPE html>
<html lang="ro">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Palangeanu Lucian, 0727 448 535">
	<title><? echo $_['logo']; ?></title>
	<link href="../css/start/jquery-ui-1.10.4.custom.css" rel="stylesheet">

	<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
	<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
	<link href="css/style.css" rel="stylesheet">
	
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
	<script src="../js/jquery-1.10.2.js"></script>
	<script src="../js/jquery-ui-1.10.4.custom.js"></script>
	<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<head>
<body>
	<div class="container">
		<h2>Adauga Statie</h2>
			<div class="tab-content">
				<?php echo $err; ?>
				<form action="" method="post">
				<div id="formAdmin">
					<div class="form-group">
						<div class="col-xs-10">
							<label>Selecteaza Cod tara</label>
							<select name="id_tara"  class="form-control" >
								<?php 
									$resTara = mysql_query("select * from tari");
									while($rowTara = mysql_fetch_array($resTara)):
								?>
								<option value="<?php echo $rowTara['id_tara']; ?>"><?php echo $rowTara['cod_tara']; ?></option>
								<?php endwhile;?>
							</select>
						</div>

						<div class="col-xs-10" >
							<label onclick="$('#adaugatara').fadeIn(100);" style="cursor:pointer;color:green"> Adauga alta tara </label>
							<input style="display:none" type="text" name="adaugaTara" id="adaugatara" class="form-control"  >
						</div>
						
						<div class="col-xs-10">
							<label>Cod statie</label>
							<input  type="text" id="" name="cod_statie" class="form-control"  placeholder="">
						</div>
						<div class="col-xs-10">
							<label>Latitudine</label>
							<input  type="text" id=""  name="lat" class="form-control"  placeholder="">
						</div>
						<div class="col-xs-10">
							<label>Longitudine</label>
							<input  type="text" id=""  name="lng" class="form-control"  placeholder="">
						</div>
						<div class="col-xs-10">
							<label>o/r</label>
							<input  type="text" id=""  name="o_r" class="form-control"  placeholder="">
						</div>
				
						<div class="col-xs-10">
								<h3> Tip Senzori</h3>
								<?php 
									$ressenzor = mysql_query("select * from tip_senzori order by tip_senzor asc");
									while($rowsenzor = mysql_fetch_array($ressenzor)):
								?>
								<input name="tipsenzor[]" type="checkbox" value="<?php echo $rowsenzor['tip_senzor'];  ?>"> <b style="color:red"><?php echo $rowsenzor['tip_senzor'];  ?></b>E <b><?php echo $rowsenzor['tip_senzor'];  ?></b>N <b><?php echo $rowsenzor['tip_senzor'];  ?></b>Z <br />
								<?php endwhile; ?>
						</div>
						
						<div class="clearfix"></div><br/><br/>
						<div class="col-xs-10">
								 <button type="submit" name="addStatie" value="add"  class="btn btn-primary" >Adauga Statie</button>
						</div>
						
						</form>
						
					</div>
				</div>
			</div>
	</div>
</body>
</html>